<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="colSpan" v-if="!itemId">
              <a-form-item label="数据表名">
                <a-input v-model="queryParam.itemTableName" placeholder="请输入项目数据表名" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="colSpan">
              <a-form-item label="审核前状态" prop="beforeStatus">
                <a-select v-if="statusOptions" placeholder="请选择审核前状态" v-model="queryParam.beforeStatus" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in beforeStatusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
                <a-cascader v-else ref="cascaderBefore" :options="selectStatusOptions" placeholder="审核前状态" @change="onChangeBeforeStatus" />
              </a-form-item>
            </a-col>
            <a-col :span="colSpan">
              <a-form-item label="审核后状态" prop="afterStatus">
                <a-select v-if="statusOptions" placeholder="请选择审核后状态" v-model="queryParam.afterStatus" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in afterStatusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
                <a-cascader v-else ref="cascaderAfter" :options="selectStatusOptions" placeholder="审核前状态" @change="onChangeAfterStatus" />
              </a-form-item>
            </a-col>
            <a-col :span="colSpan" v-if="advanced">
              <a-form-item label="审核类型" prop="afterStatus">
                <a-select placeholder="请选择审核类型" v-model="queryParam.type" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <approve-record-add-form v-if="showAddModal" ref="approveRecordAddForm" :beforeStatusOptions="beforeStatusOptions" :afterStatusOptions="afterStatusOptions" @ok="getList" @close="showAddModal = false" />
      <!-- 编辑 -->
      <approve-record-edit-form v-if="showEditModal" ref="approveRecordEditForm" :beforeStatusOptions="beforeStatusOptions" :afterStatusOptions="afterStatusOptions" @ok="getList" @close="showEditModal = false" />
      <advance-table
        title="审核记录表"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-approveRecord-index-table"
        @change="handleTableChange"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="approveRecordList"
        :loading="loading"
        :format-conditions="true"
      >
        <div class="table-operations" slot="button">
          <!-- <a-button type="primary" @click="handleAdd" v-hasPermi="['platform:approveRecord:add']"> <a-icon type="plus" />新增 </a-button>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" v-hasPermi="['platform:approveRecord:remove']"> <a-icon type="delete" />删除 </a-button> -->
          <a-button type="" @click="handleExport" v-hasPermi="['platform:approveRecord:export']"> <a-icon type="download" />导出 </a-button>
        </div>
        <span slot="type" slot-scope="{ record }">
          <a-tag :color="typeColor(record.type)">{{ typeFormat(record) }}</a-tag>
        </span>
        <span slot="beforeStatus" slot-scope="{ record }">
          <a-badge :status="statusBage(record.itemTableName, record.beforeStatus)" :text="beforeStatusFormat(record)" />
        </span>
        <span slot="afterStatus" slot-scope="{ record }">
          <a-badge :status="statusBage(record.itemTableName,record.afterStatus)" :text="afterStatusFormat(record)" />
        </span>
        <span slot="operation" slot-scope="{ record }">
          <a @click="handleUpdate(record)" v-hasPermi="['platform:approveRecord:edit']"> 修改 </a>
          <a-divider type="vertical" v-hasPermi="['platform:approveRecord:remove']" />
          <a @click="handleDelete(record)" v-hasPermi="['platform:approveRecord:remove']"> 删除 </a>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { listApproveRecord, delApproveRecord, exportApproveRecord, getInitData } from '@/api/platform/approveRecord'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import ApproveRecordAddForm from '@/views/platform/approverecord/modules/ApproveRecordAddForm'
import ApproveRecordEditForm from '@/views/platform/approverecord/modules/ApproveRecordEditForm'
export default {
  name: 'ApproveRecord',
  components: {
    AdvanceTable,
    ApproveRecordAddForm,
    ApproveRecordEditForm
  },
  props: {
    itemId: {
      type: String,
      required: false
    },
    statusOptions: {
      type: Array,
      required: false
    }
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 8 },
      // 内容区域的百分比
      wrapperCol: { span: 16 },
      colSpan: 6,
      // 审核记录表表格数据
      approveRecordList: [],
      // 审核项目前状态字典
      beforeStatusOptions: [],
      // 审核项目后状态字典
      afterStatusOptions: [],
      // 审核项目类型字典
      typeOptions: [],
      selectStatusOptions: [
        {
          value: 'region',
          dictType: 'region_status',
          label: '地区状态',
          children: [],
          options: []
        },
        {
          value: 'post',
          dictType: 'approve_status',
          label: '帖子状态',
          children: [],
          options: []
        },
        {
          value: 'comment',
          dictType: 'approve_status',
          label: '评论状态',
          children: [],
          options: []
        },
        {
          value: 'operator_apply',
          dictType: 'approve_status',
          label: '运营申请状态',
          children: [],
          options: []
        }
      ],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        itemId: this.itemId,
        itemTableName: undefined,
        beforeStatus: undefined,
        afterStatus: undefined,
        type: undefined
      },
      columns: [
        {
          title: '项目数据表名',
          dataIndex: 'itemTableName',
          ellipsis: true,
          align: 'center',
          width: '15%'
        },
        {
          title: '审核项目ID',
          dataIndex: 'itemId',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '审核项目类型',
          dataIndex: 'type',
          scopedSlots: { customRender: 'type' },
          align: 'center',
          width: '20%'
        },

        {
          title: '审核项目前状态',
          dataIndex: 'beforeStatus',
          scopedSlots: { customRender: 'beforeStatus' },
          align: 'center',
          width: '20%'
        },
        {
          title: '审核项目后状态',
          dataIndex: 'afterStatus',
          scopedSlots: { customRender: 'afterStatus' },
          align: 'center',
          width: '20%'
        },
        {
          title: '审核说明',
          dataIndex: 'reason',
          ellipsis: true,
          align: 'left',
          width: '35%'
        },
        {
          title: '审核项目时间',
          dataIndex: 'createTime',
          align: 'left',
          width: '25%'
        }
        // {
        //   title: '操作',
        //   dataIndex: 'operation',
        //   align: 'center',
        //   width: '10%',
        //   scopedSlots: { customRender: 'operation' }
        // }
      ]
    }
  },
  created() {
    this.init()
  },
  methods: {
    onChangeBeforeStatus(value) {
      this.queryParam.beforeStatus = value[1]
    },
    onChangeAfterStatus(value) {
      this.queryParam.afterStatus = value[1]
    },
    init() {
      this.getList()
      if (this.itemId) {
        const newColumns = this.columns.filter(item => item.dataIndex !== 'itemId' && item.dataIndex !== 'itemTableName')
        this.columns = newColumns
        this.colSpan = 8
        // this.labelCol = { span: 8 }
        // this.wrapperCol = { span: 16 }
      }
      if (this.statusOptions) {
        this.beforeStatusOptions = this.statusOptions
        this.afterStatusOptions = this.statusOptions
        getInitData('approve_type').then(response => {
          this.typeOptions = response.data.approve_type
        })
      } else {
        this.initOptions()
      }
    },
    initOptions() {
      let dict = ''
      this.selectStatusOptions.forEach(item => {
        dict += item.dictType + ','
      })
      getInitData(dict + 'approve_type').then(response => {
        const options = []
        this.selectStatusOptions.forEach(item => {
          options.push(this.setSelectStatusOptions(item, response.data[item.dictType]))
        })
        this.selectStatusOptions = options
        this.typeOptions = response.data.approve_type
      })
    },
    setSelectStatusOptions(item, options) {
      options.forEach(option => {
        item.children.push({ value: option.dictValue, label: option.dictLabel })
      })
      item.options = options
      return item
    },
    /** 查询审核记录表列表 */
    getList() {
      this.loading = true
      listApproveRecord(this.queryParam).then(response => {
        this.approveRecordList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },

    // 地区状态字典翻译
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    // 类型徽标数类型
    typeColor(type) {
      if (type === '0') {
        return 'green'
      } else if (type === '1') {
        return 'red'
      }
    },

    statusBage(itemTableName, status) {
      if (itemTableName === 'region') {
        return this.regionStatusBage(status)
      } else if (itemTableName === 'post') {
        return this.contentStatusBage(status)
      } else if (itemTableName === 'comment') {
        return this.contentStatusBage(status)
      }else if (itemTableName === 'operator_apply') {
        return this.operationApplyStatusBage(status)
      }
    },
    // 运营申请
    operationApplyStatusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'processing'
      } else if (status === '2') {
        return 'warning'
      } else if (status === '3') {
        return 'error'
      } else if (status === '4') {
        return 'processing'
      } else if (status === '5') {
        return 'error'
      }
     },

    // 地区状态徽标数类型
    regionStatusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'warning'
      } else if (status === '2') {
        return 'processing'
      } else if (status === '3') {
        return 'error'
      }
    },

    // 帖子状态徽标数类型
    contentStatusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'processing'
      } else if (status === '2') {
        return 'warning'
      } else if (status === '3') {
        return 'error'
      } else if (status === '4') {
        return 'processing'
      } else if (status === '5') {
        return 'error'
      }
    },

    // 审核项目前状态字典翻译
    beforeStatusFormat(row) {
      if (row.beforeStatus) {
        if (this.statusOptions) {
          return this.selectDictLabel(this.beforeStatusOptions, row.beforeStatus)
        } else {
          const statusOptions = this.selectStatusOptions.find(item => item.value === row.itemTableName)
          return this.selectDictLabel(statusOptions.options, row.beforeStatus)
        }
      } else {
        return ''
      }
    },
    // 审核项目后状态字典翻译
    afterStatusFormat(row) {
      if (row.afterStatus) {
        if (this.statusOptions) {
          return this.selectDictLabel(this.afterStatusOptions, row.afterStatus)
        } else {
          const statusOptions = this.selectStatusOptions.find(item => item.value === row.itemTableName)
          return this.selectDictLabel(statusOptions.options, row.afterStatus)
        }
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        itemId: this.itemId,
        itemTableName: undefined,
        beforeStatus: undefined,
        afterStatus: undefined,
        type: undefined
      }
      this.handleQuery()
      if (!this.statusOptions) {
        this.$refs.cascaderBefore.sValue = []
        this.$refs.cascaderAfter.sValue = []
      }
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleAdd() {
      this.showAddModal = true
      this.$nextTick(() => this.$refs.approveRecordAddForm.handleAdd())
    },
    handleUpdate(record, ids) {
      this.showEditModal = true
      this.$nextTick(() => this.$refs.approveRecordEditForm.handleUpdate(record, ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      const approveRecordIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk() {
          return delApproveRecord(approveRecordIds).then(() => {
            that.onSelectChange([], [])
            that.getList()
            that.$message.success('删除成功', 3)
          })
        },
        onCancel() {}
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk() {
          return exportApproveRecord(that.queryParam).then(response => {
            that.download(response.msg)
            that.$message.success('导出成功', 3)
          })
        },
        onCancel() {}
      })
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
